package org.grapes.wine.api.server.model.search;

import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;

import org.apache.lucene.search.Searcher;
import org.grapes.wine.api.server.model.DAOName;
import org.grapes.wine.api.server.startup.InitSessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.impl.FullTextSessionImpl;

public class HibernateIndexer {
	public static void index(DAOName name) {
		Session session = InitSessionFactory.getInstance().getCurrentSession();
		FullTextSession fullTextSession = Search.createFullTextSession(session);
		Transaction tx = fullTextSession.beginTransaction();
		List list = session.createQuery("from " + name.daoName()).list();
		for (Object object : list) {
			fullTextSession.index(object);
		}
		tx.commit();
	}
	
	public static void indexAllDAO() {
		EnumSet<DAOName> set = DAOName.getAllDAOName();
		for (Iterator it = set.iterator(); it.hasNext();) {
			DAOName daoName = (DAOName) it.next();
			index(daoName);
		}
	}
}
